/**
 * Created by Frings on 2017/5/22 product-property.
 */

/*商品属性展示*/
function ProductPropertyShow(product_property) {
    this.ProductProperty = product_property;
    this.ValueRule = JSON.parse(product_property.bom_value_rule);
    this.TempId = product_property.temp_id;
}

ProductPropertyShow.prototype = {
    getHtml: function () {
        var property_type = this.ValueRule.type;
        switch (property_type) {
            case "string":
                return this.stringHtml();
            case "number":
                return this.numberHtml();
            case "enum":
                return this.enumHtml();
            case "list":
                return this.listHtml();
            case "table":
                return this.tableHtml();
            case "file":
                return this.fileHtml();
            case "auto":
                return this.autoHtml();
        }
    },

    stringHtml: function () {
        var min_length = this.ValueRule.min_length;
        var max_length = this.ValueRule.max_length;
        var default_value = this.ProductProperty.value != '' && this.ProductProperty.value != null ? JSON.parse(this.ProductProperty.value) : {
            "value": "",
            "value_type": "string"
        };

        var obj = $("<div></div>");

        var html = '';
        html += '<div>';
        html += '<label class="control-label">最小长度：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + min_length + '" class="form-control"></div>';
        html += '<label class="control-label">最大长度：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled="disabled" value="' + max_length + '" class="form-control"></div>';
        if (this.ProductProperty.is_product_configure == 0) {
            html += '<label class="control-label">默认值：</label>';
            html += '<div class="col-lg-1"><input type="text" name="default_value" value="' + default_value.value + '" class="form-control" onblur="setProductPropertyStringValue(this)" temp-id="' + this.TempId + '"></div>';
        }
        html += '</div>';

        var obj_product_property = $(html);
        if (this.ProductProperty.is_product_configure == 1) {
            obj_product_property.hide();
            if (this.ProductProperty.value == undefined) this.ProductProperty.value = '';
            html = '<div>';
            html += '<input type="text" class="form-control" value="' + default_value.value + '" property-name="' + this.ProductProperty.name + '" temp-id="' + this.TempId + '" data-min-length="' + min_length + '" data-max-length="' + max_length + '" onblur="setProductPropertyStringValue(this)">';
            html += '</div>';

            obj.append($(html));
        }

        obj.append(obj_product_property);
        return obj[0].outerHTML;
    },

    numberHtml: function () {
        var min_length = this.ValueRule.min;
        var max_length = this.ValueRule.max;
        var step = this.ValueRule.step;
        var is_integer = this.ValueRule.is_integer;
        var default_value = this.ProductProperty.value != '' && this.ProductProperty.value != null ? JSON.parse(this.ProductProperty.value) : {
            "value": "",
            "value_type": "number"
        };

        var obj = $("<div></div>");
        var html = '';
        html += (is_integer == 1) ? '<label class="control-label" style="margin-right: 10px;">整型</label>' : '<label class="control-label" style="margin-right: 10px;">非整型</label>';
        html += '<div>';
        html += '<label class="control-label">最小值：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled value="' + min_length + '" class="form-control"></div>';
        html += '<label class="control-label">最大值：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled value="' + max_length + '" class="form-control"></div>';
        html += '<label class="control-label">变化步长：</label>';
        html += '<div class="col-lg-1"><input type="text" disabled value="' + step + '" class="form-control"></div>';
        if (this.ProductProperty.is_product_configure == 0) {
            html += '<label class="control-label">默认值：</label>';
            html += '<div class="col-lg-1"><input type="number" name="default_value" value="' + default_value.value + '" class="form-control" onblur="setProductPropertyStringValue(this)" temp-id="' + this.TempId + '"></div>';
        }
        html += '</div>';

        var obj_product_property = $(html);
        if (this.ProductProperty.is_product_configure == 1) {
            obj_product_property.hide();
            if (this.ProductProperty.value == undefined) this.ProductProperty.value = '';
            html = '<div>';
            html += '<input type="text" class="form-control" value="' + default_value.value + '" property-name="' + this.ProductProperty.name + '" temp-id="' + this.TempId + '" data-min-length="' + min_length + '" data-max-length="' + max_length + '" onblur="setProductPropertyStringValue(this)">';
            html += '</div>';

            obj.append($(html));
        }

        obj.append(obj_product_property);
        return obj[0].outerHTML;
    },

    enumHtml: function () {
        var values = this.ValueRule.values;
        var selected = (this.ProductProperty.value_rule == '') ? '' : JSON.parse(this.ProductProperty.value_rule);
        var obj = $("<div></div>");
        var html = '';
        html += '<div class="col-lg-10">';
        html += '<div>';
        for (var i = 0; i < values.length; i++) {
            values[i].is_checked = '';
            values[i].is_default = '';
            if (selected != '') {
                var selected_values = selected.values;
                var default_value = '';
                if (this.ProductProperty.value != undefined) {
                    default_value = typeof this.ProductProperty.value == 'string' ? JSON.parse(this.ProductProperty.value).value : this.ProductProperty.value.value;
                }
                for (var x = 0; x < selected_values.length; x++) {
                    if (values[i].code == selected_values[x].code) {
                        values[i].is_checked = 'checked';
                        if (default_value != '') {
                            if (selected_values[x].code == default_value.code) {
                                values[i].is_default = 'checked';
                            }
                        }
                    }
                }
            }
            html += '<label class="editor-checkbox editor-radio">';
            html += '<input type="checkbox" ' + values[i].is_checked + ' data-code="' + values[i].code + '" onclick="multiSelect(this, \'' + this.TempId + '\');">';
            html += '<span class="editor-checkbox-text">' + values[i].name + '</span>';
            html += '<input data-placement="top" data-toggle="tooltip" type="radio" ' + values[i].is_default + ' value="' + values[i].code + '" onclick="changePropertySelect(this, \'' + this.TempId + '\');" class="radio-editor" name="' + this.TempId + '">';
            html += '</label>';
        }

        html += '</div></div>';

        var obj_product_property = $(html);
        if (this.ProductProperty.is_product_configure == 1) {
            obj_product_property.hide();
            var value_code = '';
            if (this.ProductProperty.value != undefined && this.ProductProperty.value != '') {
                value_code = JSON.parse(this.ProductProperty.value).value.code;
            }
            html = '<div>';
            html += '<select class="select-group" onchange="changePropertySelect(this, \'' + this.ProductProperty.temp_id + '\');">';
            html += '<option value="">请选择</option>';
            for (var i = 0; i < values.length; i++) {
                var is_selected = values[i].code == value_code ? 'selected' : '';
                html += '<option value="' + values[i].code + '" ' + is_selected + '>' + values[i].name + '</option>';
            }
            html += '</select>';
            html += '</div>';

            obj.append($(html));
        }

        obj.append(obj_product_property);
        return obj[0].outerHTML;
    },

    listHtml: function () {
        if (arguments[0]) {
            var values = arguments[0];
            var temp_value_rule = JSON.parse(this.ProductProperty.bom_value_rule);
            temp_value_rule.values = values;
            this.ProductProperty.bom_value_rule = JSON.stringify(temp_value_rule);
        } else {
            var values = this.ValueRule.values;
        }
        var selected = (this.ProductProperty.value_rule == '') ? '' : JSON.parse(this.ProductProperty.value_rule);
        var value_id = '';
        if (this.ProductProperty.value != undefined && this.ProductProperty.value != '') {
            value_id = typeof this.ProductProperty.value == 'string' ? JSON.parse(this.ProductProperty.value).value.id : this.ProductProperty.value.value.id;
        }
        var obj = $("<div></div>");
        var html = '';
        html += '<div class="product-mod col-lg-10">';
        if (values) {
            if (values[0].url != undefined) {
                //region 有图片的list
                html += '<ul class="tag-img-select-item-wrapper">';
                for (var i = 0; i < values.length; i++) {
                    values[i].is_active = '';
                    values[i].is_default = 'default-hover';
                    values[i].txt = '设为默认';
                    if (selected != '') {
                        var selected_values = selected.values;
                        var default_value = typeof this.ProductProperty.value == 'string' ? JSON.parse(this.ProductProperty.value).value : this.ProductProperty.value.value;
                        for (var x = 0; x < selected_values.length; x++) {
                            if (values[i].id == selected_values[x].id) {
                                values[i].is_active = 'active';
                                if (selected_values[x].id == default_value.id) {
                                    values[i].is_default = 'default-state';
                                    values[i].txt = '默认';
                                    break;
                                }
                            }
                        }
                    }
                    html += '<li class="tag-img-select-item ' + values[i].is_active + '" onclick="multiSelect(this, \'' + this.TempId + '\');"';
                    html += ' data-id="' + values[i].id + '"';
                    html += ' data-url="' + values[i].url + '"';
                    html += ' data-name="' + values[i].name + '">';
                    html += '<span class="tag-img"><img src="' + values[i].url + '" alt=""></span>';
                    html += '<div class="tag-img-desc">' + values[i].name + '</div>';
                    html += '<span class="select-state"></span>';       //setImageListDefault
                    html += '<span name="is_default" class="' + values[i].is_default + '" data-id="' + values[i].id + '" onclick="setImageListDefault(this, \'' + this.ProductProperty.temp_id + '\');">' + values[i].txt + '</span>';
                    html += '</li>';
                }
                html += '</ul>';
                //endregion 有图片的list end
            } else {
                //region 无图片的list
                for (var i = 0; i < values.length; i++) {
                    values[i].is_checked = '';
                    values[i].is_default = '';
                    if (selected != '') {
                        var selected_values = selected.values;
                        var default_value = typeof this.ProductProperty.value == 'string' ? JSON.parse(this.ProductProperty.value).value : this.ProductProperty.value.value;
                        for (var x = 0; x < selected_values.length; x++) {
                            if (values[i].id == selected_values[x].id) {
                                values[i].is_checked = 'checked';
                                if (selected_values[x].id == default_value.id) {
                                    values[i].is_default = 'checked';
                                    break;
                                }
                            }
                        }
                    }
                    html += '<label class="editor-checkbox editor-radio">';
                    html += '<input type="checkbox" ' + values[i].is_checked + ' data-id="' + values[i].id + '" onclick="multiSelect(this, \'' + this.TempId + '\');">';
                    html += '<span class="editor-checkbox-text">' + values[i].name + '</span>';
                    html += '<input data-placement="top" data-toggle="tooltip" type="radio" ' + values[i].is_default + ' data-id="' + values[i].id + '" onclick="changePropertySelect(this, \'' + this.TempId + '\');" class="radio-editor" name="' + this.TempId + '">';
                    html += '</label>';
                }
                //endregion 无图片的list end
            }
        }
        html += '</div>';

        var obj_product_property = $(html);
        if (this.ProductProperty.is_product_configure == 1) {
            obj_product_property.hide();
            if (this.ProductProperty.value == undefined) this.ProductProperty.value = '';
            html = '<div>';
            if (values[0].url == undefined) {
                //region 无图片的list
                html += '<select class="select-group" onchange="changePropertySelect(this, \'' + this.ProductProperty.temp_id + '\');">';
                html += '<option value="">请选择</option>';
                for (var i = 0; i < values.length; i++) {
                    var is_selected = values[i].id == value_id ? 'selected' : '';
                    html += '<option value="' + values[i].id + '" ' + is_selected + '>' + values[i].name + '</option>';
                }
                html += '</select>';
                //endregion 无图片的list end
            } else {
                //region 有图片的list
                html += '<ul class="tag-img-select-item-wrapper">';
                for (var i = 0; i < values.length; i++) {
                    var is_active = values[i].id == value_id ? 'active' : '';
                    html += '<li class="tag-img-select-item ' + is_active + '" onclick="changeListWithImage(this, \'' + this.ProductProperty.temp_id + '\');"';
                    html += ' data-id="' + values[i].id + '"';
                    html += ' data-url="' + values[i].url + '"';
                    html += ' data-name="' + values[i].name + '">';
                    html += '<span class="tag-img"><img src="' + values[i].url + '" alt=""></span>';
                    html += '<div class="tag-img-desc">' + values[i].name + '</div>';
                    html += '<span class="select-state"></span>';
                    html += '</li>';
                }
                html += '</ul>';
                //endregion 有图片的list end
            }

            html += '</div>';


            obj.append($(html));
        }

        obj.append(obj_product_property);
        return obj[0].outerHTML;
    },

    tableHtml: function () {
        var params = {};
        var values = new Array();
        params.value_ids = new Array();
        params.table_name = this.ValueRule.table_name;
        params.columns = '';
        for (var i = 0; i < this.ValueRule.columns.length; i++) {
            params.columns += params.table_name + '.' + this.ValueRule.columns[i].key;
            if (i != this.ValueRule.columns.length - 1) {
                params.columns += ",";
            }
        }
        params.where = this.ValueRule.where;
        requestJson('../product-bom/get-property-list-by-ids-and-talbe-name-handler', params, false, function (response) {
            if (response.status == 0) {
                if (response.data) {
                    values = values.concat(response.data);
                }
            }
        });
        return this.listHtml(values);
    },

    fileHtml: function () {
        return '<label class="control-label col-lg-2">文件上传</label>';
    },

    autoHtml: function () {
        return '<label class="control-label col-lg-2">通过计算得出</label>';
    }

};


//设置属性值
function ProductProperty(ele, value_rule, property) {
    this.Ele = ele;
    this.ValueRule = typeof value_rule == 'string' ? JSON.parse(value_rule) : value_rule;
    this.PropertyType = this.ValueRule.type;
    this.ProductProperty = property;

    this.Property = null;
}

ProductProperty.prototype = {
    render: function () {
        switch (this.PropertyType) {
            case "string":
                this.Property = new ProductPropertyString(this.Ele, this.ValueRule);
                break;
            case "number":
                this.Property = new ProductPropertyNumber(this.Ele, this.ValueRule);
                break;
            case "enum":
                this.Property = new ProductPropertyEnum(this.Ele, this.ValueRule, this.ProductProperty);
                break;
            case "list":
                this.Property = new ProductPropertyList(this.Ele, this.ValueRule, this.ProductProperty);
                break;
            case "table":
                this.Property = new ProductPropertyTable(this.Ele, this.ValueRule);
                break;
            case "file":
                this.Property = new ProductPropertyFile(this.Ele, this.ValueRule);
                break;
            case "auto":
                this.Property = new ProductPropertyAuto(this.Ele, this.ValueRule);
                break;
        }
        this.Property.render();
    },
    saveValue: function () {
        if (this.Property) {
            this.ValueRule = this.Property.saveValue();
            if (this.ValueRule) {
                return true;
            }
            return false;
        }
    },
    getValue: function () {
        if (this.ValueRule) {
            return this.ValueRule;
        }
    }
};


function ProductPropertyString(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

ProductPropertyString.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';
        html += '<div class="form-horizontal" id="property_string_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min-length">最小长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min-length" data-min-length="" name="min-length" class="form-control" value=""></div>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max-length">最大长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max-length" data-max-length="" name="max-length" class="form-control" value=""></div>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min_length.toString() != "") {
                obj.find("#min-length").val(this.ValueRule.min_length);
                obj.find("#min-length").attr("data-min-length", this.ValueRule.min_length);
            } else {
                obj.find("#min-length").val("");
            }

            if (this.ValueRule.max_length.toString() != "") {
                obj.find("#max-length").val(this.ValueRule.max_length);
                obj.find("#max-length").attr("data-max-length", this.ValueRule.max_length);
            } else {
                obj.find("#max-length").val("");
            }
        }

    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min-length").val();
        var max = obj.find("#max-length").val();
        if (min != "") {
            min = parseInt(min);
        }
        if (max != "") {
            max = parseInt(max);
        }

        if (min != "" && max != "") {
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        var data_min_length = obj.find("#min-length").attr("data-min-length");
        var data_max_length = obj.find("#max-length").attr("data-max-length");

        if (data_min_length != "") {
            data_min_length = parseInt(data_min_length);
            if (min < data_min_length) {
                warningDialog("最小长度不能小于限制长度：" + data_min_length);
                return false;
            }
        }

        if (data_max_length != "") {
            data_max_length = parseInt(data_max_length);
            if (max > data_max_length) {
                warningDialog("最大长度不能大于限制长度：" + data_max_length);
                return false;
            }
        }

        this.ValueRule.type = "string";
        this.ValueRule.min_length = min;
        this.ValueRule.max_length = max;

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function ProductPropertyNumber(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

ProductPropertyNumber.prototype = {
    render: function () {
        this.Element.empty();
        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min">最小值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min-length" data-min-length="" name="min-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-min"><span>不设置最小值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max">最大值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max-length" data-max-length="" name="max-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-max"><span>不设置最大值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">变化步长：</label>';
        html += '<div class="col-lg-7"><input type="number" id="step" data-step="" name="step" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-step" data-set-step=""><span>不设置变化步长</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">是否整型：</label>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="is-integer" data-integer=""><span>是否整型</span></label>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min.toString() != "") {
                obj.find("#min-length").val(this.ValueRule.min);
                obj.find("#min-length").attr("data-min-length", this.ValueRule.min);
            } else {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
                obj.find("#no-set-min").prop("checked", true);
            }

            if (this.ValueRule.max.toString() != "") {
                obj.find("#max-length").val(this.ValueRule.max);
                obj.find("#max-length").attr("data-max-length", this.ValueRule.max);
            } else {
                obj.find("#max").val("");
                obj.find("#max").attr("disabled", true);
                obj.find("#no-set-max").prop("checked", true);
            }

            if (this.ValueRule.step.toString() != "") {
                obj.find("#step").val(this.ValueRule.step);
                obj.find("#step").attr("data-step", this.ValueRule.step);
            } else {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                obj.find("#no-set-step").prop("checked", true);
            }

            if (this.ValueRule.is_integer) {
                obj.find("#is-integer").prop("checked", true);
                obj.find("#is-integer").attr("data-integer", true);
            } else {
                obj.find("#is-integer").prop("checked", false);
                obj.find("#is-integer").attr("data-integer", false);
            }
        }

        //初始化checkbox事件
        obj.find("#no-set-min").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
                that.ValueRule.min = "";
            } else {
                obj.find("#min-length").attr("disabled", false);
            }
        });

        obj.find("#no-set-max").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#max-length").val("");
                obj.find("#max-length").attr("disabled", true);
                that.ValueRule.max = "";
            } else {
                obj.find("#max-length").attr("disabled", false);
            }
        });

        obj.find("#no-set-step").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                that.ValueRule.step = "";
            } else {
                obj.find("#step").attr("disabled", false);
            }
        });

        obj.find("#is-integer").click(function () {
            if ($(this).is(':checked')) {
                that.ValueRule.is_integer = 1;
            } else {
                that.ValueRule.is_integer = 0;
            }
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min-length").val();
        var max = obj.find("#max-length").val();
        var step = obj.find("#step").val();

        if (min != "" && max != "") {
            min = parseInt(min);
            max = parseInt(max);
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        var data_min_length = obj.find("#min-length").attr("data-min-length");
        var data_max_length = obj.find("#max-length").attr("data-max-length");

        if (data_min_length != "") {
            data_min_length = parseInt(data_min_length);
            if (min < data_min_length) {
                warningDialog("最小值不能小于限制值：" + data_min_length);
                return false;
            }
        }

        if (data_max_length != "") {
            data_max_length = parseInt(data_max_length);
            if (max > data_max_length) {
                warningDialog("最大值不能大于限制值：" + data_max_length);
                return false;
            }
        }

        this.ValueRule.type = "number";
        this.ValueRule.min = min;
        this.ValueRule.max = max;
        this.ValueRule.step = step;

        if (obj.find("#is-integer").is(":checked")) {
            this.ValueRule.is_integer = 1;
        } else {
            this.ValueRule.is_integer = 0;
        }

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function ProductPropertyEnum(element, value_rule, property) {
    this.Element = element;
    this.ValueRule = value_rule;
    this.ProductProperty = property;
}

ProductPropertyEnum.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';

        html += '<table class="table table-bordered table-hover" id="property-rule-list-table">';
        html += '<thead><tr>';
        html += '<th><input type="checkbox" class="check-box" id="checkAll"></th>';
        html += '<th>编码</th><th>名称</th>';
        html += '</tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';
        var obj = $(html);

        var that = this;
        obj.find("#checkAll").click(function () {
            if ($(this).is(':checked')) {
                that.Element.find('input[type=checkbox]').prop("checked", true);
            } else {
                that.Element.find('input[type=checkbox]').prop("checked", false);
            }
        });

        this.Element.append(obj);

        //编辑产品初始化的时候需要有list值
        if (this.ValueRule.values.length > 0) {
            var is_add_url = false; //是否添加完图片操作td
            for (var i = 0; i < this.ValueRule.values.length; i++) {
                var data = this.ValueRule.values[i];
                var html_body = '';
                html_body += '<tr>';
                var chk = '';
                var is_checked = '';
                if (this.ProductProperty.value_rule != '') {
                    var selected_value_rule = typeof this.ProductProperty.value_rule == 'string' ? JSON.parse(this.ProductProperty.value_rule) : this.ProductProperty.value_rule;
                    for (var j = 0; j < selected_value_rule.values.length; j++) {
                        if (selected_value_rule.values[j].code == data.code) {
                            is_checked = 'checked';
                            break;
                        }
                    }
                }
                chk = '<td><input type="checkbox" data-code="' + data.code + '" class="check-box" ' + is_checked + ' name="enum-checkbox"';

                if (data['url'] != undefined && data['url'] != '') {
                    chk += ' data-url="' + data['url'] + '"';
                }

                chk += ' ></td>';

                var td = '<td>' + data.code + '</td><td>' + data.name + '</td>';

                if (data['url'] != undefined && data['url'] != '') {
                    td += '<td><img style="width: 100px;height: 100px" src="' + data['url'] + '"></td>';
                    if (!is_add_url) {
                        this.Element.find("thead").find("tr").append($("<td>图片</td>"));
                        is_add_url = true;
                    }
                }

                html_body += chk + td + '</tr>';

                this.Element.find("tbody").append($(html_body));
            }
        }
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ProductProperty.value_rule = {};
        this.ProductProperty.value_rule.values = new Array();
        this.ProductProperty.value_rule.type = 'enum';
        var elements = this.Element.find("input[name='enum-checkbox']:checkbox:checked");
        if (elements.length == 0) {
            warningDialog('请选择');
            return false;
        }
        //if (this.ValueRule.values) {
        //    this.ValueRule.values.length = 0;
        //} else {
        //    this.ValueRule.values = new Array();
        //}
        for (var i = 0; i < elements.length; i++) {
            var element = $(elements[i]);
            var code = element.attr('data-code');
            for (var x = 0; x < this.ValueRule.values.length; x++) {
                if (code == this.ValueRule.values[x].code) {
                    this.ProductProperty.value_rule.values.push(this.ValueRule.values[x]);
                    break;
                }
            }
        }
        return this.ProductProperty.value_rule;
    },
    getValue: function () {
        return this.ProductProperty.value_rule;
    }
};


function ProductPropertyList(element, value_rule, property) {
    this.Element = element;
    this.ValueRule = value_rule;
    this.ProductProperty = property;
}

ProductPropertyList.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';

        html += '<table class="table table-bordered table-hover" id="property-rule-list-table">';
        html += '<thead><tr>';
        html += '<th><input type="checkbox" class="check-box" id="checkAll"></th>';
        for (var j = 0; j < this.ValueRule.columns.length; j++) {
            var column = this.ValueRule.columns[j];
            if (column.name.toLowerCase() != "id") {
                html += '<th>' + column.name + '</th>';
            }
        }

        html += '</tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';
        var obj = $(html);
        var that = this;
        obj.find("#checkAll").click(function () {
            if ($(this).is(':checked')) {
                that.Element.find('input[type=checkbox]').prop("checked", true);
            } else {
                that.Element.find('input[type=checkbox]').prop("checked", false);
            }
        });

        this.Element.append(obj);

        //编辑产品初始化的时候需要有list值
        if (this.ValueRule.values.length > 0) {
            var is_add_url = false; //是否添加完图片操作td
            for (var i = 0; i < this.ValueRule.values.length; i++) {
                var data = this.ValueRule.values[i];
                var html_body = '';
                html_body += '<tr>';

                var check_str = "";

                if (this.ProductProperty.value_rule != '') {
                    var selected_value_rule = typeof this.ProductProperty.value_rule == 'string' ? JSON.parse(this.ProductProperty.value_rule) : this.ProductProperty.value_rule;
                    for (var j = 0; j < selected_value_rule.values.length; j++) {
                        if (selected_value_rule.values[j].id == data.id) {
                            check_str = 'checked="checked"';
                            break;
                        }
                    }
                }


                var chk = '';
                chk = '<td><input type="checkbox" class="check-box" ' + check_str + ' name="list-checkbox" ';

                for (var j = 0; j < this.ValueRule.columns.length; j++) {
                    var column = this.ValueRule.columns[j];
                    // td += '<td>' + data[column.key] + '</td>';
                    chk += ' data-' + column.key + '="' + data[column.key] + '"';
                }

                if (data['url'] != undefined) {
                    chk += ' data-url="' + data['url'] + '"';
                }

                chk += '></td>';

                var td = '';
                for (var j = 0; j < this.ValueRule.columns.length; j++) {
                    var column = this.ValueRule.columns[j];
                    if (column["key"].toLowerCase() != "id") {
                        td += '<td>' + data[column.key] + '</td>';
                    }
                }

                if (data['url'] != undefined) {
                    td += '<td><img style="width: 50px;height: 50px" src="' + data['url'] + '"></td>';
                    if (!is_add_url) {
                        this.Element.find("thead").find("tr").append($("<td>图片</td>"));
                        is_add_url = true;
                    }
                }

                html_body += chk + td + '</tr>';

                this.Element.find("tbody").append($(html_body));
            }
        }
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "list";
        var elements = this.Element.find("input[name='list-checkbox']:checkbox:checked");
        if (elements.length == 0) {
            warningDialog('请选择列表项');
            return false;
        }
        if (this.ValueRule.values) {
            this.ValueRule.values.length = 0;
        } else {
            this.ValueRule.values = new Array();
        }
        for (var i = 0; i < elements.length; i++) {
            var element = $(elements[i]);
            var value = "";
            for (var j = 0; j < this.ValueRule.columns.length; j++) {
                var column = this.ValueRule.columns[j];
                value += "\"" + column["key"] + "\"" + ":" + "\"" + element.attr("data-" + column["key"]) + "\"";
                if (j < this.ValueRule.columns.length - 1) {
                    value += ",";
                }
            }
            if (element.attr("data-url") != undefined) {
                value += "," + "\"url\"" + ":" + "\"" + element.attr("data-url") + "\"";
            }

            this.ValueRule.values.push(JSON.parse("{" + value + "}"));
        }
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function ProductPropertyTable(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

ProductPropertyTable.prototype = {
    render: function () {
        this.Element.empty();

        var html = '';
        html += '<table class="table table-bordered table-hover" id="property-rule-table-table">';
        var html_thead = '<thead><tr>';
        for (var j = 0; j < this.ValueRule.columns.length; j++) {
            var column = this.ValueRule.columns[j];
            if (column.name.toLowerCase() != "id") {
                html_thead += '<th>' + column.name + '</th>';
            }
        }
        html_thead += '</tr></thread>';

        var html_body = '<tbody>';


        var params = {};
        params.table_name = this.ValueRule.table_name;
        params.columns = "";
        for (var i = 0; i < this.ValueRule.columns.length; i++) {
            params.columns += this.ValueRule.columns[i].key;
            if (i != this.ValueRule.columns.length - 1) {
                params.columns += ",";
            }
        }
        params.where = this.ValueRule.where;
        var datas = new Array();
        requestJson('../product-bom/get-datas-by-talbe-name-handler', params, false, function (response) {
            if (response.status == 0) {
                if (response.data) {
                    datas = datas.concat(response.data);
                }
            }
        });

        if (datas.length > 0) {
            for (var i = 0; i < datas.length; i++) {
                var data = datas[i];

                html_body += '<tr>';
                var td = "";
                for (var j = 0; j < this.ValueRule.columns.length; j++) {
                    var column = this.ValueRule.columns[j];
                    if (column.key.toLowerCase() != "id") {
                        td += '<td>' + data[column.key] + '</td>';
                    }
                }

                html_body += td + '</tr>';
            }
        }

        html += html_thead + html_body + '</tbody></table>';
        var obj = $(html);
        this.Element.append(obj);
    },
    saveValue: function () {
        return true;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function ProductPropertyFile(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

ProductPropertyFile.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8">文件上传</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "file";
        return this.ValueRule;
    },
    getValue: function () {

        return this.ValueRule;
    }
};


function ProductPropertyAuto(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

ProductPropertyAuto.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8">使用公式进行计算</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (this.ValueRule) {
            this.ValueRule = {};
        }

        this.ValueRule.type = "auto";
        return true;
    },
    getValue: function () {

        return this.ValueRule;
    }
};